package cn.xtxfan.trigger.http;

import cn.xtxfan.domain.plan.model.dto.CheckPlanDTO;
import cn.xtxfan.domain.plan.model.valobj.CheckPlanVO;
import cn.xtxfan.domain.plan.service.ICheckPlanService;
import cn.xtxfan.domain.plan.model.dto.CheckAddPlanDTO;
import cn.xtxfan.types.enums.ResponseCode;
import cn.xtxfan.types.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;

/**
 * @className: PlanController
 * @author: XTX
 * @date: 2025/7/19 19:31
 * @Version: 1.0
 * @description:
 */
@RestController
@RequestMapping("/check/plan")
@Slf4j
@CrossOrigin("*")
public class PlanController {

    @Resource
    private ICheckPlanService planService;

    @GetMapping("/list")
    public Response<CheckPlanVO> getPlanList(@NotEmpty @RequestParam("userId") String userId) {
        log.info("获取用户的计划列表：{}", userId);
        try {
            CheckPlanVO checkPlanVO = planService.queryUserPartnerPlans(userId);
            return Response.<CheckPlanVO>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(checkPlanVO)
                    .build();
        } catch (Exception e) {
            log.error(e.toString());
            return Response.<CheckPlanVO>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @PostMapping("/finish/{planId}")
    public Response finishPlan(@NotEmpty @PathVariable("planId") String planId) {
        log.info("修改任务状态: {}", planId);
        try {
            planService.finishPlan(planId);

            return Response.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } catch (Exception e) {
            log.error(e.toString());
            return Response.builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }


    @PostMapping("/delete/{planId}")
    public Response delPlan(@NotEmpty @PathVariable("planId") String planId) {
        log.info("删除任务: {}", planId);
        try {
            planService.delPlan(planId);
            return Response.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } catch (Exception e) {
            log.error(e.toString());
            return Response.builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }


    @PostMapping("/add")
    public Response addPlan(@RequestBody CheckAddPlanDTO checkAddPlanDTO) {
        log.info("添加任务：{}", checkAddPlanDTO);
        try {
            planService.addPlan(checkAddPlanDTO);
            return Response.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } catch (Exception e) {
            log.error(e.toString());
            return Response.builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }


}

